/*
 * @Author: your name
 * @Date: 2021-07-22 14:21:46
 * @LastEditTime: 2021-07-23 05:07:59
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: /szjzglfxgl/src/api/login.js
 */
import request from "../request/request";
import {dealPhoneNum} from "../utils";
import store from '../store'

// 登陆微信获取temporarySecretKey，用于后续获取用户手机号
export async function wxLogin(code) {
  // let wxlogin = new Promise((resolve)=>{
  //   uni.login({
  //     provider: "weixin",
  //     success: function (res) {
  //       store.commit('SET_WXCODE',res.code)
  //       resolve(res.code);
  //     },
  //   });
  // })
  // let code = await wxlogin
  console.log('登陆微信获取temporarySecretKey，用于后续获取用户手机号code=', code);
  return request({
    url: '/user/wechat/login',
    method: 'get',
    data: { 
      code
    }
  })
}

// 微信手机号授权后，将encryptedData,iv两个参数传入，获取用户手机号码 purePhoneNumber
export async function getWxBindPhone({encryptedData, iv, temporarySecretKey}) {
  return request({
    url: '/user/wechat/decode-data',
    method: 'POST',
    data: {
      encryptedData,
      iv,
    },
    header: {
      "Temporary-Secret-Key": temporarySecretKey,
      "Content-Type": "application/json",
    }
  })
}

// 从深i您获取手机号码
export function getPhoneBySIN(code) {
  return request({
    url: '/shen-ai-nin/user',
    method: 'get',
    data:{
      code
    },
  })
}

// 使用手机号码登陆系统
export function login(phone, hotelId, isGldWorker) {
  let data = {
    phone: dealPhoneNum(phone), 
    timestamp: Date.parse(new Date()),
    isGldWorker
  }
  if(hotelId) data.hotelId = hotelId
  return new Promise((resolve, reject)=>{
    request({
      url: '/user/login',
      method: 'POST',
      data
    }).then(rs=>{
      uni.setStorageSync('token', rs.sessionid);
      uni.setStorageSync('personalInfo', rs);
      console.log('setStorage', uni.getStorageSync("personalInfo"))
      resolve(rs)
    }, error=>{
      reject(error)
    })
  })
  
}
//检查是管理员还是隔离人员
export function userCheck(params) {
  console.log('userCheck', params)
  return request({
    url: `/person-info/user-check`,
    method: 'POST',
    data:params
  })
}
// 获取登录账户信息
export function getGzryInfo(){
  return request({
    url: `/user/info`,
    method: 'GET',
  })
}
// 登出接口
export function logout () {
  return request({
    url: '/user/logout',
    method: 'POST'
  })
}
